<?
include("../functions/functions.php");                                                                                        

if (isset($_POST['Year'])) {
	$thisYear = $_POST['Year']	;
}
else {
	$thisYear = date("Y");
}

$lastYear = $thisYear-1;
$reportTitle = "Unique visitors for the financial year of $lastYear/$thisYear shown in the postcode of there first visit.";

$totalClients = 1;
$tempPostcode = 0;
$postcode = 0;
$total = 0;
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title><? echo $reportTitle; ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="reports.css" rel="stylesheet" type="text/css">
</head>

<body bgcolor="#FFFFFF" text="#000000" link="#0075C5" vlink="#0075C5" alink="#C5DDF4" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<?                                                                                                                                                                                                                
echo "<p style=\"text-align:center; font-size: 16pt\">$reportTitle</p>";
echo "<p>To find associated suburbs with postcodes please visit http://www.auspost.com.au/postcodes/</p>";
?>
<table width="100%" border="0">
	<tr>
		<td width="50%">
			<form method="post"> 
				Select the financial year to report on: <select name="Year" onChange="submit();">
					<?
					$years = getBeginEndYears();
					for ($j = $years[start]; $j<$years[end]; $j++) {
						$option = ($j-1)." - ".$j;
						if ($thisYear == $j)
							$select = "SELECTED";
						else
							$select = "";
						echo "<option value=\"$j\" $select>$option</option>";
					}
					?>
				</select>
			</form>
		</td>
		<td align="right" width="50%"><? echo getCurrentDate(); ?></td>
	</tr>
</table>
<?

echo "<p>Each person counted once in a financial year in the first month they presented for ER</p>";

echo "<table width=\"100%\" cellpadding=\"3\" cellspacing=\"0\" border=\"1\" bordercolor=\"#000000\" class=\"body\" FRAME=BOX RULES=GROUPS>";

echo "<tbody><tr>
				<td class=\"bodybold\" style=\"width:50%;\">Site Name</td>
				<td class=\"bodybold\" style=\"width:50%;\">Site Total</td>
			</tr></tbody>";

$sql = "SELECT er_visit.client_id, min(er_visit.visit_date) as firstVisit, er_visit.postcode
				FROM er_visit 
				WHERE er_visit.visit_date >= '$lastYear-07-01' AND er_visit.visit_date <= '$thisYear-06-30' 
				GROUP BY er_visit.client_id
				ORDER BY er_visit.postcode";
//echo $sql;

$results = r_db($sql);

while ($row = mysql_fetch_array($results)) {
	$tempClientID = $row["client_id"];
	$tempFirstVisit = $row["firstvisit"];
	$tempPostcode = $row["postcode"];
	
	if ($postcode != $tempPostcode) {

		$postcode = $tempPostcode;
		echo "<tbody><tr>";
		echo "<td>$postcode</td><td>$totalClients</td>";
		echo "</tr></tbody>";
		$total += $totalClients;
		$totalClients = 1;
	}
	else {
		$totalClients++;
	}
}
echo "</table>";

echo "Total unique clients - $total";

?>
</body>
</html>